<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>微信公共账号</title>
<script type="text/javascript">
	var $grid;
	var editRow = true;
	var selectRow = -1;
	$(function() {
		$grid = $("#grid").datagrid(
				$.extend(datagridOption, {
					//title : '微信公共账号',
					url : "${ctx}/wx/app/dataGrid",
					fitColumns : true,
					sortName : "originAppId",
					sortOrder : "desc",
					columns : [ [
							{
								field : 'ck',
								checkbox : true
							},
							{
								field : 'originAppId',
								title : '应用ID',
								sortable : true,
								editor : {
									type : "validatebox",
									options : {
										required : true
									}
								}
							},
							{
								field : 'appName',
								title : '应用名称',
								sortable : true,
								width : 50,
								editor : {
									type : "validatebox",
									options : {
										required : true
									}
								}
							},
							{
								field : 'appNo',
								title : '微信号',
								sortable : true,
								width : 50,
								editor : {
									type : "validatebox",
									options : {
										required : true
									}
								}
							},
							{
								field : 'appType',
								title : '账号类型',
								sortable : true,
								width : 50,
								editor : {
									type : "combobox",
									options : {
										required : true,
										editable : false,
										url : kl.ctx + '/pub/combobox/appType',
										textField : "text",
										valueField : "value"
									}
								}
							},
							{
								field : 'appDesc',
								title : '介绍',
								sortable : true,
								width : 50,
								editor : {
									type : "validatebox",
									options : {
										required : true
									}
								}
							},
							{
								field : 'isAuth',
								title : '认证情况',
								sortable : true,
								formatter : function(value, row, index) {
									return '<input class="easyui-switchbutton" ' + (value == 1 ? "checked" : "")
											+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
								}
							}, {
								field : 'address',
								title : '地址',
								sortable : true,
								width : 50,
								editor : "text"
							}, {
								field : 'loginEmail',
								title : '登陆邮箱',
								sortable : true,
								width : 50,
								editor : {
									type : "validatebox",
									options : {
										required : true,
										validType : 'email'
									}
								}
							}, {
								field : 'appSecret',
								title : '应用密钥',
								sortable : true,
								editor : {
									type : "validatebox",
									options : {
										required : true
									}
								}
							}, {
								field : 'appToken',
								title : 'appToken',
								sortable : true,
								editor : {
									type : "validatebox",
									options : {
										required : true
									}
								}
							}, {
								field : 'aesKey',
								title : 'aesKey',
								sortable : true,
								editor : "text"
							}, {
								field : 'accessToken',
								title : 'accessToken',
								sortable : true,
								editor : "text"
							}, {
								field : 'expiresIn',
								title : 'expiresIn',
								sortable : true,
								editor : "text"
							} ] ],
					onDblClickRow : function(rowIndex, rowData) {
						if (selectRow != -1) {
							$(this).datagrid("endEdit", selectRow);
						}
						selectRow = rowIndex;
					},
					onBeforeEdit : function(rowIndex, rowData, changes) {
					},
					onAfterEdit : function(rowIndex, rowData, changes) {
						$.fn.datagrid.extensions.onAfterEdit.apply(this, arguments); //这句一定要加上
						$(this).datagrid("updateRow", {
							index : rowIndex,
							row : rowData
						});
						editRow = true;
					},
					autoEditingEvent : "onDblClickRow",
					autoEditing : true, //该属性启用双击行时自定开启该行的编辑状态
					extEditing : true, //该属性启用行编辑状态的 ExtEditing 风格效果，该属性默认为 true。
					singleEditing : true
				}));

		$("#grid").datagrid('enableFilter', [ {
			field : 'isAuth',
			javaType : 'N',
			op : [ 'EQ', 'NEQ', 'GT', 'LT', 'GTE', 'LTE', 'ISNULL', 'NOTNULL' ]
		} ]);

		$("#isAuthSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#isAuth").val(1);
				} else {
					$("#isAuth").val(2);
				}
			}
		});

		fn_initComboNotId("appType", "appType", true);//app类型

		$("#add").click(function() {
			$("#fm").form("clear");
			$('#fm').form('load', {
				isAuth : 1
			});

			$("#isAuthSB").switchbutton('check');
			$("#dlg").dialog("open").dialog("setTitle", "新增微信公共账号");
		});
		$("#edit").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (!rowData) {
				showInfo("您未选中行，无法编辑");
			} else {
				$("#dlg").dialog("open").dialog("setTitle", "编辑微信公共账号");
				$("#fm").form("clear");
				$("#fm").form("load", rowData);
				$("#isAuthSB").switchbutton(rowData.isAuth == 1 ? "check" : "uncheck");
			}
		});
		$("#delete").click(function() {
			multiDeleteDataGrid($grid, "${ctx}/wx/app/multiDelete");
		});
		$("#search").click(function() {
			$grid.datagrid('load', $("#tt").form("getData"));
		});

		$("#excelExport").click(function() {
			var url = "${ctx}/wx/app/excelExport?" + $.param($grid.datagrid('options').queryParams);
			if ($grid.datagrid("options").filterRules) {
				url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
			}
			window.parent.location.href = url;
		});
		$("#getUserList").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (!rowData) {
				showInfo("请先选中相应应用！");
				return false;
			}
			var url = "${ctx}/wx/wxUser/synUserList";
			$.post(url, {
				appId : rowData.originAppId
			}, function(data) {
				if (data.success) {
					$.messager.show({
						msg : data.msg,
						icon : "info",
						position : "bottomRight"
					});
					//showInfo(data.msg)
				}
			}, "json");
		});

		$("#getGroupList").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (!rowData) {
				showInfo("请先选中相应应用！");
				return false;
			}
			var url = "${ctx}/wx/groups/synGroupList";
			$.post(url, {
				appId : rowData.originAppId,
				id : rowData.id
			}, function(data) {
				if (data.success) {
					$.messager.show({
						msg : data.msg,
						icon : "info",
						position : "bottomRight"
					});
				}
			}, "json");
		});

		$("#getMenu").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (!rowData) {
				showInfo("请先选中相应应用！");
				return false;
			}
			var url = "${ctx}/wx/wxMenu/synMenu";
			$.post(url, {
				appId : rowData.originAppId
			}, function(data) {
				if (data.success) {
					$.messager.show({
						msg : data.msg,
						icon : "info",
						position : "bottomRight"
					});
					//showInfo(data.msg)
				}
			}, "json");
		});
	});
</script>
</head>

<body>
	<div data-options="region:'center',fit:true,border:false">
		<table id="grid">
		</table>
	</div>
	<div id="tt" class="row">
		<div class="col-md-4">
			<form class="form-grid-search form-inline">
				<div class="form-group">
					<input type="text" class="form-control"
						name="search_LIKE_appName" placeholder="应用名称" />
				</div>
				<button type="button" id="search" class="btn btn-primary">
					<span class="glyphicon glyphicon-search"></span> 查询
				</button>
			</form>
		</div>
		<div class="col-md-8">
			<shiro:hasPermission name="app:update">
				<button id="add" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-plus"></span> 新增
				</button>
				<button id="edit" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-pencil"></span> 编辑
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="app:delete">
				<button id="delete" type="button" class="btn btn-danger">
					<span class="glyphicon glyphicon-minus"></span> 删除
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="app:export">
				<button id="excelExport" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-save"></span> Excel导出
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="wxUser:getUserList">
				<button id="getUserList" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-user"></span> 同步用户
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="wxUser:getGroupList">
				<button id="getGroupList" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-cloud"></span> 同步分组
				</button>
			</shiro:hasPermission>
			<button id="getMenu" type="button" class="btn btn-primary">
				<span class="glyphicon glyphicon-th-list"></span> 同步菜单
			</button>
		</div>
	</div>
	<shiro:hasPermission name="app:update">
		<div id="dlg" class="easyui-dialog"
			style="width: 670px; height: 405px; padding: 10px 20px"
			data-options="resizable:true,modal:true, buttons:'#dlg-btn',closed:true">
			<form id="fm" method="post">
				<input type="hidden" name="id" />
				<table id="tbl">
					<tr>
						<td><label>应用ID:</label></td>
						<td><input name="originAppId" class="easyui-validatebox"
							data-options="required: true"></td>
						<td><label>应用名称:</label></td>
						<td><input name="appName" class="easyui-validatebox"
							data-options="required: true"></td>
					</tr>
					<tr>
						<td><label>微信号:</label></td>
						<td><input name="appNo" class="easyui-validatebox"
							data-options="required: true"></td>
						<td><label>账号类型:</label></td>
						<td><input id="appType" name="appType"></td>
					</tr>
					<tr>
						<td><label>介绍:</label></td>
						<td><input name="appDesc" class="easyui-validatebox"
							data-options="required: true"></td>
						<td><label>认证情况:</label></td>
						<td><input id="isAuth" name="isAuth" type="hidden"> <input
							id="isAuthSB"></td>
					</tr>
					<tr>
						<td><label>地址:</label></td>
						<td><input name="address" class="easyui-validatebox"></td>
						<td><label>登陆邮箱:</label></td>
						<td><input name="loginEmail" class="easyui-validatebox"
							data-options="required: true,validType: 'email'"></td>
					</tr>
					<tr>
						<td><label>应用密钥:</label></td>
						<td><input name="appSecret" class="easyui-validatebox"
							data-options="required: true"></td>
						<td><label>appToken:</label></td>
						<td><input name="appToken" class="easyui-validatebox"
							data-options="required: true"></td>
					</tr>
					<tr>
						<td><label>aesKey:</label></td>
						<td><input name="aesKey" class="easyui-validatebox"></td>
						<td><label>accessToken:</label></td>
						<td><input name="accessToken" class="easyui-validatebox"></td>
					</tr>
					<tr>
						<td><label>expiresIn:</label></td>
						<td><input name="expiresIn" class="easyui-validatebox"></td>
						<td><label>oauth2redirectUri:</label></td>
						<td><input name="oauth2redirectUri"
							class="easyui-validatebox"></td>
					</tr>
				</table>
			</form>
			<div id="dlg-btn">
				<button id="save" type="button" class="btn btn-primary"
					onclick="formSave('#fm', '#grid', '#dlg', '${ctx}/wx/app/update');">
					<span class="glyphicon glyphicon-ok"></span> 保存
				</button>
				<button id="close" type="button" class="btn btn-primary"
					onclick="$('#dlg').dialog('close');">
					<span class="glyphicon glyphicon-remove"></span> 取消
				</button>
			</div>
		</div>
	</shiro:hasPermission>
</body>
</html>
